package cn.sykj.car.controller;


import cn.sykj.car.common.ResponseBean;
import cn.sykj.car.common.StatusCode;
import cn.sykj.car.entity.Dictionary;
import cn.sykj.car.entity.Users;
import cn.sykj.car.service.IDictionaryService;
import cn.sykj.car.service.IUsersService;
import cn.sykj.car.vo.DictionaryVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author yuhao
 * @since 2018-12-06
 */
@Api(hidden = false, tags = {"字典表 前端控制器"})
@RestController
@Slf4j
@RequestMapping("/dictionary")
public class DictionaryController extends BaseController {

    // 日志
    private static Logger logger = LoggerFactory.getLogger(DictionaryController.class);

    @Resource
    private IDictionaryService iDictionaryService;

    @Resource
    private IUsersService iUsersService;

    /**
     * 新增分类
     * @param dictionary
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "新增字典表", notes = "新增字典表")
    @RequiresAuthentication
    @RequiresPermissions("dictionary:create")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResponseBean addDictionary(@RequestBody Dictionary dictionary) throws Exception {

        //获取登录系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        logger.debug((String) obj);

        //设置创建时间
        dictionary.setCreateTime(new Date());
        //设置修改时间
        dictionary.setUpdateTime(new Date());
        Users userByName=iUsersService.getUserByName((String) obj);
        //设置创建人
        dictionary.setCreateUser(userByName.getUserName());
        //设置修改人
        dictionary.setUpdateUser(userByName.getUserName());

        Boolean aBoolean = iDictionaryService.addDictionary(dictionary);
        this.writeLog("新增字典");
        return new ResponseBean(StatusCode.OK,aBoolean);
    }

    /**
     * 修改字典表
     * @param dictionary
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "修改字典表", notes = "修改字典表")
    @RequiresAuthentication
    @RequiresPermissions("dictionary:edit")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseBean updateGoodsCategory(@RequestBody Dictionary dictionary) throws Exception {

        //获取登录系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        logger.debug((String) obj);

        //设置修改时间
        dictionary.setUpdateTime(new Date());
        //设置修改人
        Users userByName=iUsersService.getUserByName((String) obj);
        dictionary.setUpdateUser(userByName.getUserName());

        Boolean aBoolean = iDictionaryService.updateDictionary(dictionary);
        this.writeLog("修改字典");
        return new ResponseBean(StatusCode.OK,aBoolean);
    }

    /**
     * 删除字典表
     * @param id
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "删除字典表", notes = "删除字典表")
    @RequiresAuthentication
    @RequiresPermissions("dictionary:delete")
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
    public ResponseBean deleteGoodsCategory(@PathVariable Integer id) throws Exception {

        //获取登录系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        logger.debug((String) obj);

        Boolean a= iDictionaryService.deleteDictionary(id);
        this.writeLog("删除字典");
        return new ResponseBean(StatusCode.OK,a);
    }

    /**查询字典表
     *
     *
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "查看字典表", notes = "登陆陆作")
    @RequiresAuthentication
    @RequiresPermissions("dictionary:view")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public ResponseBean getPage(
            @RequestBody Dictionary dictionary,
            @RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer currentPage,
            @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize
    ) throws Exception {

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        logger.debug((String) obj);

        IPage<DictionaryVO> page=new Page<>(currentPage,pageSize);
        page=iDictionaryService.pageDictionary(page,dictionary);

        return new ResponseBean(StatusCode.OK,page);
    }

    /**
     * 查看所有权限：登陆后才能操作
     *
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "查看所有权限", notes = "登陆才能操作")
    @ApiImplicitParams({
    })
    @RequiresAuthentication
    @RequiresPermissions("dictionary:view")
    @RequestMapping(value = "/{dictGroup}", method = RequestMethod.POST)
    public ResponseBean getGroup(@PathVariable String dictGroup) throws Exception {

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        logger.debug((String) obj);
        List<Dictionary> list = iDictionaryService.getListByGroup(dictGroup);

        // 获取全部角色
        return new ResponseBean(StatusCode.OK, list);
    }


}
